// components/search/module/sPicker/sPicker.js
const { debounce } = require("../../../../utils/util");
Component({
  options: {
    // virtualHost: true,
    styleIsolation: "apply-shared",
  },
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Object,
      value: () => {
        return {};
      },
    },
    value: {
      type: null,
      value: "",
    },
  },
  observers: {
    data(v) {
      if (v.type === "multistepPicker") {
        let arr = [],
          obj = {},
          columns = [];
        v.list?.forEach((item) => {
          arr.push({
            title: item.title,
            value: item.value,
          });
          obj[item.value] = [
            {
              title: "全部",
            },
            ...item.junior,
          ];
        });
        columns = [
          {
            values: arr,
            className: v.names[0],
          },
          {
            values: obj[arr[0]?.value],
            className: v.names[1],
          },
        ];
        this.setData({
          columns,
          citys: obj,
        });
      }
    },
    value(v) {
      let { list, type } = this.data.data;
      if (type === "pickerDate") {
        let insideValue;
        if (v[0] === "custom") {
          insideValue = (v[1] || "") + "至" + (v[2] || "");
        } else {
          insideValue = list?.find((i) => i.value === v[0])?.title || "";
        }
        this.setData({
          insideValue,
        });
      } else if (type === "pickerShop") {
        this.setData({
          insideValue: v ? list.find((i) => i.shop_id === v).shop_name : "",
          pValue: v ? list.findIndex((i) => i.shop_id === v) : 0,
        });
      } else if (type === "pickerCoach") {
        this.setData({
          insideValue: v ? list.find((i) => i.coach_id === v).nickname : "",
          pValue: v ? list.findIndex((i) => i.coach_id === v) : 0,
        });
      } else if (type === "multistepPicker") {
        let { columns, citys } = this.data;
        let pValues = [0, 0],
          insideValue = "";
        if (v[0]) {
          pValues[0] = columns[0].values.findIndex((i) => i.value === v[0]);
          insideValue = columns[0].values[pValues[0]].title;
        }
        columns[0].defaultIndex = pValues[0];
        columns[1].values = citys[columns[0]?.values[pValues[0]]?.value] || [];
        if (v[1]) {
          pValues[1] = columns[1].values.findIndex((i) => i.value === v[1]) ;
          insideValue += "/" + columns[1].values[pValues[1]].title;
        }
        columns[1].defaultIndex = pValues[1];
        this.setData({
          columns,
          pValues,
          insideValue
        })
      } else {
        this.setData({
          insideValue: v ? list.find((i) => i.value === v).title : "",
          pValue: v ? list.findIndex((i) => i.value === v) : 0,
        });
      }
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    insideValue: "",
    pValue: 0,
    pValues: [0, 0],
    columns: null,
    citys: null,
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onClose() {
      this.setData({
        show: false,
      });
    },
    openPicker(e) {
      this.setData({
        show: true,
      });
    },
    changeVal(e) {
      if (this.data.data.type === "multistepPicker") {
        const { picker, value, index } = e.detail;
        if (!index) {
          picker.setColumnValues(1, this.data.citys[value[0].value]);
          picker.setColumnIndex(1, 0);
        }
        this.setData({
          pValues: [picker.getColumnIndex(0), picker.getColumnIndex(1)],
        });
      } else {
        this.setData({
          pValue: e.detail.index,
        });
      }
    },
    getValue(e) {
      let { name, list, type } = this.data.data;
      let { pValue, pValues, columns } = this.data;
      if (type === "picker") {
        this.setData({
          insideValue: list[pValue].title,
          show: false,
        });
        this.triggerEvent("value", {
          type: name,
          value: list[pValue].value,
        });
      } else if (type === "multistepPicker") {
        let text = [];
        columns.forEach((item, index) => {
          if (item.values[pValues[index]].value) {
            text.push(item.values[pValues[index]].title);
            this.triggerEvent("value", {
              type: item.className,
              value: item.values[pValues[index]].value,
            });
          }
        });
        this.setData({
          insideValue: text.join("/"),
          show: false,
        });
        // this.triggerEvent("value", {
        //   type: e.detail.type,
        //   value: e.detail.value,
        // });
      } else {
        this.triggerEvent("value", {
          type: e.detail.type,
          value: e.detail.value,
        });
      }
    },
  },
});
